package com.wude.guild.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.umeng.analytics.MobclickAgent;
import com.wude.guild.ListTrafficActivity;
import com.wude.guild.LoginActivity;
import com.wude.guild.MainActivity;
import com.wude.guild.R;
import com.wude.guild.dialog.DoubleButtonDialog;
import com.wude.guild.face.DoubleButtonFace;
import com.wude.guild.face.LuckViewFace;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.guild.views.LuckPanView;
import com.wude.tools.Util;
import com.wude.tools.Utils;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 简版福利
 */

public class WelfareFragment2 extends TitleBaseFragment implements View.OnClickListener, LuckViewFace, DoubleButtonFace {
    private LinearLayout ll_traffic;
    private LinearLayout ll_luckPan;
    private LuckPanView luckPanView;

    private MainActivity activity;
    private DoubleButtonDialog dialog;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.welfarefragment_2, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("WelfareFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("WelfareFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setHeaderTitle("福利");
        getTitleHeaderBar().getLeftImageView().setVisibility(View.GONE);
        initView(view);
    }

    private void initView(View view) {
        ll_traffic = view.findViewById(R.id.ll_traffic);
        ll_luckPan = view.findViewById(R.id.ll_luckPan);

        ll_traffic.setOnClickListener(this);
        luckPanView = new LuckPanView(getContext(), this, this);
        ll_luckPan.addView(luckPanView);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (22 == requestCode && Utils.getLoginUser() != null) {
            startActivity(new Intent(getActivity(), ListTrafficActivity.class));
        }
    }


    @Override
    public void onClick(View view) {
        if (Utils.getLoginUser() == null) {
            startActivityForResult(new Intent(getActivity(), LoginActivity.class), 22);
        } else {
            startActivity(new Intent(getActivity(), ListTrafficActivity.class));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (MainActivity) activity;
    }

    Util util = Util.getInstance();

    @Override
    public void isFirst() {
        //判断是否是第一次    如果是则弹窗提示规则
//        activity.refreshRedIcon();//刷新红点
        if (activity.dayLootery == 0 && util.getIsFree(getContext()) == 0) {// 不是第一次抽奖  展示弹窗
            dialog = new DoubleButtonDialog(getContext(), this);
            dialog.show();
        } else if (activity.dayLootery == 1) {//当天第一次抽奖  改变弹窗展示状态为
            util.setIsFree(getContext(), 0);
            luckPanView.rotation();
        } else {
            luckPanView.rotation();
        }
        activity.refreshRedIcon();//刷新红点
    }

    @Override
    public void kown() {//知道了  下次点击展示弹窗
        dialog.dismiss();
//        luckPanView.canGoOn(false);
        luckPanView.canGoOn(true);
        //改变弹窗状态为  要显示
        util.setIsFree(getContext(), 0);
    }

    @Override
    public void goon() {//继续抽奖  当日不再展示弹窗
        dialog.dismiss();
        luckPanView.canGoOn(true);
        util.setIsFree(getContext(), 1);
    }
}
